package com.ho1ho.camera2live.utils

import android.content.Context
import android.graphics.Point
import android.media.MediaCodecList
import android.util.DisplayMetrics
import android.view.WindowManager

/**
 * Created by Michael Leo
 * Version 1.0 2016/03/21 09:50
 */
object DeviceUtil {
    private const val TAG = "DeviceUtil"

    fun getResolution(ctx: Context): Point {
        val wm = ctx.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        val display = wm.defaultDisplay
        val size = Point()
        display.getSize(size)
        return size
    }

    @Suppress("unused")
    fun getResolutionWithVirtualKey(ctx: Context): Point {
        val size = Point()
        val wm = ctx.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        val display = wm.defaultDisplay
        val dm = DisplayMetrics()
        val c: Class<*>
        try {
            c = Class.forName("android.view.Display")
            val method = c.getMethod("getRealMetrics", DisplayMetrics::class.java)
            method.invoke(display, dm)
            size.x = dm.widthPixels
            size.y = dm.heightPixels
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return size
    }

    @Suppress("unused")
    fun getEncoderListByMimeType(mimeType: String) = MediaCodecList(MediaCodecList.ALL_CODECS)
            .codecInfos.filter { it.isEncoder }
            .filter { it.supportedTypes.indexOfFirst { type -> type.equals(mimeType, true) } > -1 }

    @Suppress("unused")
    fun hasEncoderByCodecName(mimeType: String, codecName: String) =
            getEncoderListByMimeType(mimeType).indexOfFirst { it.name.equals(codecName, true) } > -1
}